﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VatTuDTO;
using VatTuBUS1;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace CungUngXayDung.admin
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        private SanPhamDTO sp = new SanPhamDTO();
        string tenHinh;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadDanhSachLoaiSP();
                string catid = Request.QueryString["catid"].ToString();
                int masp = Convert.ToInt32(catid);
                if (masp != 0)
                {
                    LayThongTinSanPham(masp);
                }
            }
            catch { }
        }
        //Lay THong TIn san Pham theo MaSanPham
        void LayThongTinSanPham(int masp)
        {
            sp = new SanPhamBUS().SelectSanPham(masp);
            txtTenSanPham.Text = sp.TenSanPham.ToString();
            txtTenNhaSanXuat.Text = sp.TenNhaSanXuat.ToString();

            txtDonViTinh.Text = sp.DonViTinh.ToString();
            txtGiaBan.Text = sp.GiaBan.ToString();
            txtMoTa.Text = sp.MoTa.ToString();

            txtSoLuongTon.Text = sp.SoLuongTon.ToString();
            txtTenHinh.Text = sp.HinhAnh.ToString();
            lblHinh.Text = sp.HinhAnh.ToString();
            imgHinh.ImageUrl = "~/images/SanPham/" + lblHinh.Text;

            txtAnHien.Text =sp.AnHien.ToString();
            ddlLoaiSP.DataValueField = sp.MaLoaiSanPham.ToString();  
            Dispose();
        }

        //---------------------------------
        void LoadDanhSachLoaiSP()
        {
            List<LoaiSanPhamDTO> lst = new List<LoaiSanPhamDTO>();
            lst = new LoaiSanPhamBUS().SelectLoaiSanPham();
            ddlLoaiSP.DataSource = lst;
            ddlLoaiSP.DataValueField = "MaLoaiSanPham";
            ddlLoaiSP.DataTextField = "TenLoaiSanPham";
            ddlLoaiSP.DataBind();
            Dispose();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = "QLSanPham.aspx";
            Response.Redirect(url);  
        }

        //su ly cap nhat lai san pham
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string catid = Request.QueryString["catid"].ToString();
            int dk = Convert.ToInt32(catid);
            if (dk != 0)
            {
                lblThemSua.Text = "Chọn ảnh khác";
                txtTenHinh.Text = tenHinh;
                sp.TenSanPham = Request.Form[2].ToString();
                sp.TenNhaSanXuat = Request.Form[3].ToString();
                sp.DonViTinh = Request.Form[4].ToString(); 
                sp.GiaBan =int.Parse( Request.Form[5].ToString()); 
                sp.MoTa = Request.Form[6].ToString();
                sp.SoLuongTon = int.Parse(Request.Form[7].ToString());
                //sp.HinhAnh = Request.Form[8].ToString();
                sp.HinhAnh = txtTenHinh.Text;
                sp.MaLoaiSanPham = int.Parse(Request.Form[9].ToString());
                sp.AnHien= int.Parse(Request.Form[10].ToString());
                new SanPhamBUS().UpdateSanPham(sp);
                string url = "QLSanPham.aspx";
                Response.Redirect(url);
            }
            if (dk == 0)
            {
                lblThemSua.Text = "Chọn ảnh ";
                sp.TenSanPham = Request.Form[2].ToString();
                sp.TenNhaSanXuat = Request.Form[3].ToString();
                sp.DonViTinh = Request.Form[4].ToString();
                sp.GiaBan = int.Parse(Request.Form[5].ToString());
                sp.MoTa = Request.Form[6].ToString();
                sp.SoLuongTon = int.Parse(Request.Form[7].ToString());        
                sp.HinhAnh = Request.Form[8].ToString();    
                sp.MaLoaiSanPham = int.Parse(Request.Form[9].ToString());
                sp.AnHien = int.Parse(Request.Form[10].ToString());

                new SanPhamBUS().InsertSanPham(sp);
                string url = "QLSanPham.aspx";
                Response.Redirect(url);
            }
        }
        //Up hinh len server
       
        protected void Button1_Click(object sender, EventArgs e)
        {
             
            String mess = "";
            if (checkFile(FileUpload1, ref mess))
            {
                String SaveDir = @"images\SanPham\";
                //String SaveDir = @"Thumn\";
                // String oSaveDir = @"Images\";
                String appPath = Request.PhysicalApplicationPath;
                String savePath = appPath + SaveDir + Server.HtmlEncode(FileUpload1.FileName);
                //  String osavePath = appPath + oSaveDir + Server.HtmlEncode(FileUpload1.FileName);
                Bitmap OldBMP = new Bitmap(FileUpload1.FileContent);
                int newX = 0;
                int newY = 0;
                int newHeight = 300;
                int newWidth = 300;
                int oHeight = OldBMP.Height;
                int oWidth = OldBMP.Width;
                int ratio = oWidth / oHeight;

                // Neu kich thuoc hinh nho hon kich thuoc can resize thi
                //khong can resize nua ma luu vao luon
                if (oHeight <= 500 && oWidth <= 500)
                {
                    FileUpload1.SaveAs(savePath);
                    // FileUpload1.SaveAs("D:\\");
                    //   FileUpload1.SaveAs(osavePath);
                    messages.Text = "Đã up " + tenHinh +"lên ";

                    
                    tenHinh = FileUpload1.FileName;
                    txtTenHinh.Text = tenHinh;
                    imgHinh.ImageUrl = "~/images/SanPham/" + tenHinh;
                    return;
                }

                Bitmap newBMP = new Bitmap(OldBMP, newWidth, newHeight);
                Graphics gr = Graphics.FromImage(newBMP);

                gr.SmoothingMode = SmoothingMode.AntiAlias;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;

                newBMP.Save(savePath);
                // newBMP.Save("D:\\");
                OldBMP.Dispose();
                gr.Dispose();
                newBMP.Dispose();
                messages.Text = "Upload successed" + tenHinh;
                tenHinh = FileUpload1.FileName;
                txtTenHinh.Text = tenHinh;
                imgHinh.ImageUrl = "~/images/SanPham/" + tenHinh;
            }
            else
                messages.Text = "Error: " + mess;

        }

        protected Boolean checkFile(FileUpload fu, ref String mess)
        {

            if (!fu.HasFile)
            {
                mess = "No file selected.";
                return false;

            }
            //Kiem tra dinh dang
            String ex = Path.GetExtension(fu.FileName);

            Boolean file_ex = false;
            Boolean file_size = false;

            switch (ex.ToLower())
            {
                case ".gif":
                    file_ex = true;
                    break;
                case ".jpg":
                    file_ex = true;
                    break;
                case ".bmp":
                    file_ex = true;
                    break;
                case ".png":
                    file_ex = true;
                    break;

            }
            if (!file_ex)
            {
                mess = "File format extension is not accepted.";
            }

            if (!(file_size = (fu.PostedFile.ContentLength < 1024000)))
            {
                mess += "Your file is larger than 1MB";
            }
            return (file_size && file_ex);
        }
            
    }
}